package com.wiz.web.controllor;

import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.List;

import javax.annotation.Resource;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.reflect.TypeToken;
import com.wiz.web.service.HqStationService;
import com.wiz.web.service.UpdateService;
import com.wiz.web.vo.AdminGroupHistoryVO;
import com.wiz.web.vo.criteria.FpCriteria;
import com.wiz.web.vo.criteria.HqStationCriteria;
import com.wiz.web.vo.criteria.ListCriteria;
import com.wiz.web.vo.to.AdminTO;
import com.wiz.web.vo.to.OilTankTO;

@SuppressWarnings("all")
@Controller
@RequestMapping("/hs")
public class HqStationControllor extends BaseControllor{
	private transient final Logger log = LoggerFactory.getLogger(getClass());
	
	@Resource
	private HqStationService hqStationService;
	@Resource
	private UpdateService updateService;

	/**
	 * 集团所有会员等级信息列表
	 */
	@RequestMapping(value = "/grades", method = RequestMethod.POST)
	@ResponseBody
	public String getGrades(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String hqstation = null;
		try{
			hqstation = getJsonStrByParam(stationId, params);
			HqStationCriteria criteria = gson.fromJson(hqstation, new TypeToken<HqStationCriteria>(){}.getType());
			return hqStationService.getGradesByHqId(criteria);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("getGrades error in "+stationId+"  params="+hqstation+"  exception="+e.toString(),e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	/**
	 * 积分兑换列表
	 */
	@RequestMapping(value = "/points", method = RequestMethod.POST)
	@ResponseBody
	public String getPointList(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String hqstation = null;
		try{
			hqstation = getJsonStrByParam(stationId, params);
			HqStationCriteria criteria = gson.fromJson(hqstation, new TypeToken<HqStationCriteria>(){}.getType());
			return hqStationService.getPointList(criteria);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("getPointList error in "+stationId+"  params="+hqstation+"  exception="+e.toString(),e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	/**
	 * 积分兑换
	 */
	@RequestMapping(value = "/expoint", method = RequestMethod.POST)
	@ResponseBody
	public String exchangePoint(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String hqstation = null;
		try{
			hqstation = getJsonStrByParam(stationId, params);
			HqStationCriteria criteria = gson.fromJson(hqstation, new TypeToken<HqStationCriteria>(){}.getType());
			criteria.setStationId(stationId);
			criteria.setHqId(hqId);
			return hqStationService.exchangePoint(criteria);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("exchangePoint error in "+stationId+"  params="+hqstation+"  exception="+e.toString(),e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	/**
	 * 油枪配置
	 */
	@RequestMapping(value = "/fpconfig", method = RequestMethod.POST)
	@ResponseBody
	public String configFpsByStation(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String hqstation = null;
		try{
			hqstation = getJsonStrByParam(stationId, params);
			ListCriteria<FpCriteria> listCriteria = ListCriteria.fromJson(hqstation, FpCriteria.class);
			List<FpCriteria> criteriaList = listCriteria.getDataList();
			return hqStationService.configFpsByStation(criteriaList);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("configFpsByStation error in "+stationId+"  params="+hqstation+"  exception="+e.toString(),e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	/**
	 * 油枪监控
	 */
	@RequestMapping(value = "/fpmonitor", method = RequestMethod.POST)
	@ResponseBody
	public String fpMonitor(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String hqstation = null;
		try{
			hqstation = getJsonStrByParam(stationId, params);
			ListCriteria<FpCriteria> listCriteria = ListCriteria.fromJson(hqstation, FpCriteria.class);
			List<FpCriteria> criteriaList =  listCriteria.getDataList();
			return hqStationService.updateFp(criteriaList);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("fpMonitor error in "+stationId+"  params="+hqstation+"  exception="+e.toString(),e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	/**
	 * 交接班(上下)
	 */
	@RequestMapping(value = "/group", method = RequestMethod.POST)
	@ResponseBody
	public String insertAdminGroup(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String hqstation = null;
		try{
			hqstation = getJsonStrByParam(stationId, params);
			ListCriteria<AdminTO> listCriteria = ListCriteria.fromJson(hqstation, AdminTO.class);
			List<AdminTO> adminList = listCriteria.getDataList();
			return hqStationService.insertAdminGroup(adminList);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("insertAdminGroup error in "+stationId+"  params="+hqstation+"  exception="+e.toString(),e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	/**
	 * 上下班
	 */
	@RequestMapping(value = "/attend", method = RequestMethod.POST)
	@ResponseBody
	public String insertAttend(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String hqstation = null;
		try{
			hqstation = getJsonStrByParam(stationId, params);
			AdminTO to = gson.fromJson(hqstation, new TypeToken<AdminTO>(){}.getType());
			return hqStationService.insertAttendHistory(to);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("insertAttend error in "+stationId+"  params="+hqstation+"  exception="+e.toString(),e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	/**
	 * 上传相关内容
	 * @param myfile
	 * @param memberId
	 * @param type
	 * @param request
	 * @return
	 * @throws IOException
	 */
//	@RequestMapping(value="/upload/group", method=RequestMethod.POST) 
//	@ResponseBody
//    public String addUser(@RequestParam("myfile")MultipartFile myfile,@RequestParam("stationId")String stationId,
//    		HttpServletRequest request){ 
//		return updateService.updateAdminGroupFile(myfile, stationId, request);
//    }
	
	@RequestMapping(value = "/group/sync", method = RequestMethod.POST)
	@ResponseBody
	public String syncAdminGroupHistory(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String hqstation = null;
		try{
			hqstation = getJsonStrByParam(stationId, params);
			AdminGroupHistoryVO vo = gson.fromJson(hqstation, new TypeToken<AdminGroupHistoryVO>(){}.getType());
			return hqStationService.syncAdminGroupHistory(vo,hqId,stationId);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("syncAdminGroupHistory error in "+stationId+"  params="+hqstation+"  exception="+e.toString(),e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
	
	@RequestMapping(value = "/stations", method = RequestMethod.POST)
	@ResponseBody
	public String getStationsByHqId(HttpServletRequest request,
			@RequestParam(value = "params") String params,
			@RequestParam(value = "hqId") String hqId,
			@RequestParam(value = "stationId") String stationId,
			HttpSession session, Model model){
		String hqstation = null;
		try{
			hqstation = getJsonStrByParam(stationId, params);
			HqStationCriteria criteria = gson.fromJson(hqstation, new TypeToken<HqStationCriteria>(){}.getType());
			return hqStationService.getStationsByHqId(criteria);
		}catch(BadPaddingException | NoSuchPaddingException | InvalidKeyException  |
				NoSuchAlgorithmException | InvalidKeySpecException | InvalidAlgorithmParameterException |
				IllegalBlockSizeException ke){
			return RESPONSE_CODE_KEY_ERROR;
		}catch (Exception e) {
			e.printStackTrace();
			log.error("getStationsByHqId error in "+stationId+"  params="+hqstation+"  exception="+e.toString(),e);
			return RESPONSE_CODE_SERVER_ERROR;
		}
	}
}
